//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class Tokaplot22 extends BroadcastReceiver
{
	private static final String TAG = "Tokaplot";
	
	@Override
	public void onReceive(Context context, Intent intent) { 
	    
        final String action = intent.getAction();
	    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {   	
	    	
	    	Bundle extras = intent.getExtras();
	    	if (extras == null)
	    		return; // Handed us a null bundle for some reason
	    	
	    	int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
	    	if (appWidgetIds == null)
	    		return; // Handed us a null appWidgetIds for some reason
	    	
	    	// Create start intent
	    	Intent widgetStartIntent;
	    	for (int i=0; i<appWidgetIds.length; i++) {
	    		
		    	widgetStartIntent = new Intent(context, TokaplotService.class);
		    	widgetStartIntent.putExtra(TokaplotService.EXTRA_APPWIDGET_FLAGS, TokaplotService.ACTION_WIDGET_START);
		    			    	
		    	try {
			    	// Create widget start info
			    	WidgetInfo wInfo = new WidgetInfo(appWidgetIds[i], 22);
			    	wInfo.setIntentWithInfo(widgetStartIntent);
			    	
			    	// Start widget thread
			    	context.startService(widgetStartIntent);
		    	} catch(Exception e) {
		    		
		    		// Catch any exceptions
		    		Log.e(TAG, "Exception: " + e.getMessage());
		    		Intent widgetStopIntent = new Intent(context, TokaplotService.class);
		    		context.stopService(widgetStopIntent);
		    	}
	    	}
		    
	    } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { 
	    	
	    	Bundle extras = intent.getExtras();
	    	if (extras == null)
	    		return; // Handed us a null bundle for some reason
	    	
	    	int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
	    	
	    	// Create stop widget intent
	    	Intent widgetStopIntent = new Intent(context, TokaplotService.class);
	    	widgetStopIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
	    	widgetStopIntent.putExtra(TokaplotService.EXTRA_APPWIDGET_FLAGS, TokaplotService.ACTION_WIDGET_STOP);
		    	
	    	// Stop widget thread
	    	context.startService(widgetStopIntent);
	    }
	}
}